package com.library.dao;

import com.library.model.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDAO extends BaseDAO {

    // 多条件查询图书
    public List<Book> findByCondition(String title, String author, String isbn, String publisher) {
        List<Book> books = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        StringBuilder sql = new StringBuilder("SELECT * FROM book WHERE 1=1");
        List<String> params = new ArrayList<>();

        if (title != null && !title.trim().isEmpty()) {
            sql.append(" AND title LIKE ?");
            params.add("%" + title + "%");
        }
        if (author != null && !author.trim().isEmpty()) {
            sql.append(" AND author LIKE ?");
            params.add("%" + author + "%");
        }
        if (isbn != null && !isbn.trim().isEmpty()) {
            sql.append(" AND isbn LIKE ?");
            params.add("%" + isbn + "%");
        }
        if (publisher != null && !publisher.trim().isEmpty()) {
            sql.append(" AND publisher LIKE ?");
            params.add("%" + publisher + "%");
        }

        try {
            conn = getConnection(); // 使用BaseDAO的连接方法
            pstmt = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pstmt.setString(i + 1, params.get(i));
            }
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setIsbn(rs.getString("isbn"));
                book.setTitle(rs.getString("title"));
                book.setAuthor(rs.getString("author"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setCreatedAt(rs.getTimestamp("created_at"));
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstmt, rs); // 使用BaseDAO的关闭方法
        }
        return books;
    }

    // 根据ID查询图书
    public Book findById(int id) {
        Book book = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM book WHERE id = ?";

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                book = new Book();
                book.setId(rs.getInt("id"));
                book.setIsbn(rs.getString("isbn"));
                book.setTitle(rs.getString("title"));
                book.setAuthor(rs.getString("author"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setCreatedAt(rs.getTimestamp("created_at"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstmt, rs);
        }
        return book;
    }

    // 新增图书
    public int add(Book book) {
        String sql = "INSERT INTO book (isbn, title, author, publisher, publish_date) VALUES (?, ?, ?, ?, ?)";
        try {
            // 使用BaseDAO的通用更新方法
            return executeUpdate(sql,
                    book.getIsbn(),
                    book.getTitle(),
                    book.getAuthor(),
                    book.getPublisher(),
                    new java.sql.Date(book.getPublishDate().getTime())
            );
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 更新图书
    public int update(Book book) {
        String sql = "UPDATE book SET isbn=?, title=?, author=?, publisher=?, publish_date=? WHERE id=?";
        try {
            return executeUpdate(sql,
                    book.getIsbn(),
                    book.getTitle(),
                    book.getAuthor(),
                    book.getPublisher(),
                    new java.sql.Date(book.getPublishDate().getTime()),
                    book.getId()
            );
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 删除图书
    public int delete(int id) {
        String sql = "DELETE FROM book WHERE id=?";
        try {
            return executeUpdate(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
